﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.GOLD;
using Mang24k99.Business.GOLD;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;
using Castle.ActiveRecord;

namespace Mang24k99.Web.ProductQuote
{
    public partial class OrglistProduct : System.Web.UI.Page
    {
        //产品归属类别：1-金商，2-银行
        protected string product_type = "1";
        //金商和银行维护产品数量限制
        protected string GoldSellerProductNum = BKtCommBaseInfo.GetValue("GoldSellerProductNum");
        protected string  BankProductNum = BKtCommBaseInfo.GetValue("BankProductNum");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["procduct_type"] != null)
            {
                product_type = Request.QueryString["procduct_type"].ToString();
                if (product_type == "1")
                {
                    this.sort.Visible=false;
                }
            }
            if (!IsPostBack)
            {
                this.Panel2.Visible = false;
                DateBind();
            }
        }

        private void DateBind()
        {
            BindOrglist();
            BindGoldType();
            BindProduct();
            BindGoldUnit();
            BindPage();
            BindGridView();
        }
        /// <summary>
        /// 根据传入的不同参数显示页面
        /// </summary>
        private void BindPage()
        {
            if (ddlGoldType.SelectedValue=="")
            {
                string strMsg = "金品品种为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                this.Panel4.Visible = false;
                return;
            }
            //当产品所属类别为银行
            if (product_type=="2")
            {
                trWeight.Visible = true;     //重量
                trUnit.Visible = true;         //规格
                grdList.Columns[3].Visible = true;    //列表重量列
                grdList.Columns[4].Visible = true;    //列表规格列
                grdList.Columns[5].Visible = true;    //内页排序
                grdList.Columns[6].Visible = true;    //列表编辑列
                Panel4.Visible = true;    //排序确定与取消按钮
            }
            //当产品所属类别为金商
            if (product_type=="1")
            {  //黄金、铂金、钯金
                if ((ddlGoldType.SelectedValue == "30201" || ddlGoldType.SelectedValue == "30202" || ddlGoldType.SelectedValue == "30203"))
                {
                    trWeight.Visible = false;
                    trUnit.Visible = false;
                    grdList.Columns[3].Visible = false;
                    grdList.Columns[4].Visible = false;
                    grdList.Columns[5].Visible = false;
                    grdList.Columns[6].Visible = false;    //列表编辑列
                    Panel4.Visible = false;
                }
                if ((ddlGoldType.SelectedValue == "30204" || ddlGoldType.SelectedValue == "30205" || ddlGoldType.SelectedValue == "30206"))
                {
                    trWeight.Visible = true;     //重量
                    trUnit.Visible = true;         //规格
                    grdList.Columns[3].Visible = true;    //列表重量列
                    grdList.Columns[4].Visible = true;    //列表规格列
                    grdList.Columns[5].Visible = true;    //内页排序
                    grdList.Columns[6].Visible = true;    //列表编辑列
                    Panel4.Visible = true;    //排序确定与取消按钮
                }
            }
            
        }
        /// <summary>
        /// 点击新增金品按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (ddlGoldType.SelectedValue==""||ddlOrglist.SelectedValue=="")
            {
                string strMsg = "金品品种或机构为空，不能新增！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(ValidatePage()==false)
            {
                return;
            }
            if (btnSave.Text=="增加")
            {
                ProductAdd();
            }
            if (btnSave.Text=="更新")
            {
                ProductUpdate();
            }
        }
        private void ProductUpdate()
        {
            if (Session["productno"]!=null)
            {
                int productno = Convert.ToInt32(Session["productno"].ToString());
                MKtGoldProduct goldproduct = BKtGoldProduct.Find(productno);
                int weight=Convert.ToInt32(txtWeight.Text.Trim());
                int codeno=Convert.ToInt32(ddlStandardNO.SelectedValue);
                //如果修改了则验证产品是否已经维护
                if (goldproduct.MEASUREMENT!=weight || goldproduct.MKtCommCodelist.CODE_NO!=codeno)
                {
                    if (ProductValidate()==false)
                    {
                        return;
                    }
                }
                if (txtWeight.Text.Trim() != "")
                {
                    goldproduct.MEASUREMENT = Convert.ToInt32(txtWeight.Text.Trim());
                }
                //单位
                MKtCommCodeList codelist = new MKtCommCodeList();
                codelist.CODE_NO = Convert.ToInt32(ddlStandardNO.SelectedValue);
                goldproduct.MKtCommCodelist = codelist;
                goldproduct.ModifyTime = DateTime.Now;
                try
                {
                    goldproduct.Update();
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    BindGridView();
                    PageRefresh();
                }
                catch (System.Exception e)
                {
                    string strMsg = "更新失败！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }
            
        }
        /// <summary>
        /// 机构维护产品数量限制(tag=1.新增时判断，tag=2排序时判断)
        /// </summary>
        /// <returns></returns>
        public bool ProductNumValidate(int tag)
        {
            int orglistno = Convert.ToInt32(ddlOrglist.SelectedValue);
            int goldtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            MKtGoldProduct[] goldproductlist = (MKtGoldProduct[])BKtGoldProduct.SelectByWhere(orglistno, goldtype);
            if (product_type=="1")
            {
                int num = 5;
                if (GoldSellerProductNum!=null && Common.PageValidate.IsNumber(GoldSellerProductNum))
                {
                    num = Convert.ToInt32(GoldSellerProductNum);
                }
                if (goldtype == 30204 || goldtype == 30205 || goldtype == 30206)
                {
                    if (tag==1)
                    {
                        if (goldproductlist.Length >= num)
                        {
                            string strMsg = "您维护的产品数已经达到上限"+num+"个，不能再维护！";
                            Common.MessageBox.Show(this.Page, strMsg);
                            return false;
                        }
                    }
                    else
                    {
                        if (goldproductlist.Length >num)
                        {
                            string strMsg = "您维护的产品数超出上限"+num+"个，不允许排序！";
                            Common.MessageBox.Show(this.Page, strMsg);
                            return false;
                        }
                    }
                }
            }
            if (product_type=="2")
            {
                int num = 5;
				if (BankProductNum != null && Common.PageValidate.IsNumber(BankProductNum))
                {
                    num = Convert.ToInt32(BankProductNum);
                }

                if (tag == 1)
                {
                    if (goldproductlist.Length >= num)
                    {
                        string strMsg = "您维护的产品数已经达到上限" + num + "个，不能再维护！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return false;
                    }
                }
                else
                {
                    if (goldproductlist.Length > num)
                    {
                        string strMsg = "您维护的产品数超出上限" + num + "个，不允许排序！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return false;
                    }
                }
            }
            return true;
        }

        private void ProductAdd()
        {
            if (ProductValidate() == false)
            {
                return;
            }
            if (ProductNumValidate(1)==false)
            {
                return;
            }
            MKtGoldProduct goldproduct = new MKtGoldProduct();
            //primarykey
            goldproduct.ProcductNO = MKtGoldProduct.GetMaxProductNO() + 1;
            int orglistno = Convert.ToInt32(ddlOrglist.SelectedValue);
            int goldtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            //机构编号
            MKtCommOrglist orglist = BKtCommOrglist.Find(orglistno);
            goldproduct.MKtCommOrglist = orglist;
            //品种编号
            MKtGoldGoldbreed goldbreed = BKtGoldGoldbreed.Find(Convert.ToInt32(ddlGoldName.SelectedValue));
            goldproduct.MKtGoldGoldbreed = goldbreed;
            //单位
            MKtCommCodeList codelist = new MKtCommCodeList();
            codelist.CODE_NO = Convert.ToInt32(ddlStandardNO.SelectedValue);
            goldproduct.MKtCommCodelist = codelist;
            goldproduct.PageOrder = (int)MKtGoldProduct.GetMaxSortNO(orglist.OrgnizeNO, Convert.ToInt32(ddlGoldType.SelectedValue))+1;
            if (txtWeight.Text.Trim()!="")
            {
                goldproduct.MEASUREMENT = Convert.ToInt32(txtWeight.Text.Trim());
            }
            
            goldproduct.ProductBelong = Convert.ToInt32(product_type);
            goldproduct.IsValidate = 1;
            goldproduct.Is_Mporder = 0;
            goldproduct.CloseShow = goldbreed.CloseShow;
            //指明一对一的关系
            MKtGoldSaleprice saleprice = new MKtGoldSaleprice();
            goldproduct.MKtGoldSaleprice = saleprice;
            saleprice.MKtGoldProduct = goldproduct;
            saleprice.IsValidate = 1;
            saleprice.LastsalePrice = 0;
            saleprice.PriceDatetime = DateTime.Now;
            saleprice.InputTime = DateTime.Now;
            goldproduct.InputTime = DateTime.Now;
            try
            {
                MKtGoldProduct.Create(goldproduct);
                string strMsg = "新增成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindGridView();
                PageRefresh();
            }
            catch (System.Exception ex)
            {

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            PageRefresh();
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        public void  PageRefresh()
        {
            this.txtWeight.Text = "";
            this.btnSave.Text = "增加";
            ddlGoldName.Enabled = true;
            ddlOrglist.Enabled = true;
            ddlGoldType.Enabled = true;
            Session["productno"] = "";
        }
        /// <summary>
        /// 绑定机构
        /// </summary>
        public void BindOrglist()
        {
            string where=string.Empty;
            if (product_type=="2")
            {
                //银行机构
                where = " orglist.ParentNO=3 and orglist.IsValidate=1 and orglist.IsColumn=1 order by orglist.LevelOrder";
            }
            else 
            {
                //金商机构
                where = " orglist.ParentNO=2 and orglist.IsValidate=1 and orglist.IsColumn=1 order by orglist.LevelOrder";
            }
            MKtCommOrglist[] orglist = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
            this.ddlOrglist.DataSource = orglist;
            this.ddlOrglist.DataTextField = "OrgnizeName";
            this.ddlOrglist.DataValueField = "OrgnizeNO";
            this.ddlOrglist.DataBind();
        }

        /// <summary>
        /// 绑定金品分类
        /// </summary>
        public void BindGoldType()
        {
            MKtCommCodeList[] codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldTypeByBelong(Convert.ToInt32(product_type));
            this.ddlGoldType.DataSource = codelist;
            this.ddlGoldType.DataTextField = "CODE_CNAME";
            this.ddlGoldType.DataValueField = "CODE_NO";
            this.ddlGoldType.DataBind();
        }

        /// <summary>
        /// 绑定金品单位
        /// </summary>
        public void BindGoldUnit()
        {
            MKtCommCodeList[] codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldUnit();
            this.ddlStandardNO.DataSource = codelist;
            this.ddlStandardNO.DataTextField = "CODE_CNAME";
            this.ddlStandardNO.DataValueField = "CODE_NO";
            this.ddlStandardNO.DataBind();
        }
        /// <summary>
        /// 绑定产品
        /// </summary>
        public void BindProduct()
        {
            if (ddlGoldType.SelectedValue=="")
            {
                return;
            }
            int breedtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            MKtGoldGoldbreed[] breedlist = (MKtGoldGoldbreed[])BKtGoldGoldbreed.SelectByType(breedtype, Convert.ToInt32(product_type),0);
            this.ddlGoldName.DataSource = breedlist;
            this.ddlGoldName.DataTextField = "BreedName";
            this.ddlGoldName.DataValueField = "BreedNO";
            this.ddlGoldName.DataBind(); 
        }
        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BindGridView()
        {
            BindPage();
            if (ddlOrglist.SelectedValue!=""&&ddlGoldType.SelectedValue!="")
            {
                int orglistno = Convert.ToInt32(this.ddlOrglist.SelectedValue);
                int goldtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
                MKtGoldProduct[] goldproductlist = (MKtGoldProduct[])BKtGoldProduct.SelectByWhere(orglistno, goldtype);
                if (goldproductlist.Length == 0)
                {
                    this.Panel4.Visible = false;
                }
                this.grdList.DataSource = goldproductlist;
                this.grdList.DataBind();
            } 
        }
        /// <summary>
        /// 金品品种下拉选项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlGoldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProduct();
            PageRefresh();
            BindPage();
            BindGridView();
        }
        /// <summary>
        /// 页面验证
        /// </summary>
        /// <returns></returns>
        public bool ValidatePage()
        {
            //当产品所属类别为银行并且选择的品种大类为金条、金章时
            //or产品所属类别为金商并且选择的品种大类为金条、金币、银条时
            if (((ddlGoldType.SelectedValue == "30204" || ddlGoldType.SelectedValue == "30207") && product_type == "2")
                   || ((ddlGoldType.SelectedValue == "30204" || ddlGoldType.SelectedValue == "30205" || ddlGoldType.SelectedValue == "30206") && product_type == "1"))
            {
                if (ddlStandardNO.SelectedValue=="")
                {
                    string strMsg = "单位信息未维护，请先维护单位！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
                if (txtWeight.Text.Trim() == "")
                {
                    string strMsg = "请输入重量！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
                if (Common.PageValidate.IsNumber(txtWeight.Text.Trim()) == false)
                {
                    string strMsg = "重量输入格式不对，请输入数字！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
            }
          
            return true;
        }
        /// <summary>
        /// 机构名称下拉选项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlOrglist_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageRefresh();
            BindGridView();
        }

        /// <summary>
        /// 验证金品是否已经维护
        /// </summary>
        /// <returns></returns>
        public bool ProductValidate()
        {
            int orglistno = Convert.ToInt32(ddlOrglist.SelectedValue);
            int breedno = Convert.ToInt32(ddlGoldName.SelectedValue);
            //当产品所属类别为银行并且选择的品种大类为金条、金章时
            //or产品所属类别为金商并且选择的品种大类为金条、金币、银条时
            if (((ddlGoldType.SelectedValue == "30204" || ddlGoldType.SelectedValue == "30207") && product_type == "2")
                   || ((ddlGoldType.SelectedValue == "30204" || ddlGoldType.SelectedValue == "30205" || ddlGoldType.SelectedValue == "30206") && product_type == "1"))
            {
                int measurement = Convert.ToInt32(txtWeight.Text.Trim());
                int unitno = Convert.ToInt32(ddlStandardNO.SelectedValue);
                MKtGoldProduct[] productlist = (MKtGoldProduct[])BKtGoldProduct.SelectProductBySpec(orglistno, breedno, measurement, unitno);
                if (productlist.Length > 0)
                {
                    string strMsg = "该类型的金品已经维护！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
                return true;
            }
            else
            {
                string breedname = ddlGoldName.SelectedItem.Text;
                MKtGoldProduct[] productlist = (MKtGoldProduct[])BKtGoldProduct.SelectProduct(orglistno, breedno);
                if (productlist.Length > 0)
                {
                    string strMsg = "(" + breedname + ")已经维护！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
                return true;
            }
        }
        protected void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = Convert.ToInt32(e.RowIndex);
            int productno = Convert.ToInt32(grdList.Rows[index].Cells[1].Text);
            MKtGoldProduct product =(MKtGoldProduct) BKtGoldProduct.Find(productno);
            try
            {
                product.Delete();
                string strMsg = "删除成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindGridView();
                PageRefresh();
            }
            catch (System.Exception ex)
            {
                string strMsg = "删除失败！";
                Common.MessageBox.Show(this.Page, strMsg);
            }
            
        }
        /// <summary>
        /// 点击编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "modi")
            {
                int productno = Convert.ToInt32(e.CommandArgument.ToString());
                MKtGoldProduct product = (MKtGoldProduct)BKtGoldProduct.Find(productno);
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                ddlGoldName.SelectedValue = product.MKtGoldGoldbreed.BreedNO.ToString();
                txtWeight.Text = product.MEASUREMENT.ToString();
                ddlStandardNO.SelectedValue = product.MKtCommCodelist.CODE_NO.ToString();
                btnSave.Text = "更新";
                ddlGoldName.Enabled = false;
                ddlOrglist.Enabled = false;
                ddlGoldType.Enabled = false;
                Session["productno"] = product.ProcductNO;
            }
        }
        /// <summary>
        /// 内页排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (ProductNumValidate(2) == false)
            {
                return;
            }
            int count = this.grdList.Rows.Count;
            for (int i = 0; i < count;i++ )
            {
                int NO = Int32.Parse(this.grdList.Rows[i].Cells[1].Text);
                MKtGoldProduct product = BKtGoldProduct.Find(NO);
                GridViewRow gvr = grdList.Rows[i];
                TextBox txtSort = (TextBox)gvr.FindControl("txtSort");
                if (txtSort.Text.Trim()=="")
                {
                    string strMsg = "排序号不能为空，位置第"+ (i+1) +"行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                if (Common.PageValidate.IsNumber(txtSort.Text.Trim())==false)
                {
                    string strMsg = "排序号必须为正整数，位置第" + (i + 1) + "行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                if (Convert.ToInt32(txtSort.Text.Trim())==0)
                {
                    string strMsg = "排序号不能为0，位置第" + (i + 1) + "行";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                product.PageOrder = Convert.ToInt32(txtSort.Text.Trim());
                try
                {
                    product.Update();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            string Msg = "修改排序成功！";
            Common.MessageBox.Show(this.Page, Msg);
            BindGridView();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            BindGridView();
        }
    }
}
